package bb.lanxing.calc.calculator;

import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.DurationUtil;
import bb.lanxing.util.Log;

public class PauseCalc {
    final static private String TAG = "PauseCalc";
    private static final int NOT_CYCLING_COUNT = 3;
    private static final float NOT_CYCLING_SPEED = 0.5555556f;
    private static final int NOT_MOVING_COUNT = 3;
    private static final float NOT_MOVING_SPEED = 0.2777778f;
    private static final float NOT_RUNING_SPEED = 0.2777778f;
    private static final int NOT_RUNNING_COUNT = 7;
    private static final int NOT_TRAINING_COUNT = 2;
    private static final int NOT_INDOORBIKE_COUNT = 2;
    private static final int NOT_CROSSTRAINER_COUNT = 2;
    private static final int NOT_BIKETRAINER_COUNT = 2;
    private static final int NOT_ROWER_COUNT = 2;
    private static final int NOT_WALKING_COUNT = 10;
    private static final float NOT_WALKING_SPEED = 0.2777778f;
    private SourcePoint lastValidPoint;
    private int notMovingCount = 0;

    public static int getNotMovingCount(int sportType) {
        if (sportType == Workout.TYPE_WALKING) {
            return NOT_WALKING_COUNT;
        } else if (sportType == Workout.TYPE_RUNNING) {
            return NOT_RUNNING_COUNT;
        } else if (sportType == Workout.TYPE_TRAINING) {
            return NOT_TRAINING_COUNT;
        } else if (sportType == Workout.TYPE_INDOOR_BIKE) {
            return NOT_INDOORBIKE_COUNT;
        } else if (sportType == Workout.TYPE_CROSS_TRAINER) {
            return NOT_CROSSTRAINER_COUNT;
        } else if (sportType == Workout.TYPE_BIKE_TRAINER) {
            return NOT_BIKETRAINER_COUNT;
        } else if (sportType == Workout.TYPE_ROWER) {
            return NOT_ROWER_COUNT;
        } else {
            return NOT_MOVING_COUNT;
        }
    }

    public static float getNotMovingSpeed(int sportType) {
        if (sportType == Workout.TYPE_CYCLING) {
            return NOT_CYCLING_SPEED;
        } else {
            return NOT_WALKING_SPEED;
        }
    }

    public boolean isPaused(SourcePoint sourcePoint) {
        if (lastValidPoint == null) {
            lastValidPoint = sourcePoint;
        }
        int sportType = RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING);
        if ((sportType == Workout.TYPE_TRAINING)
            || (sportType == Workout.TYPE_INDOOR_BIKE)
            || (sportType == Workout.TYPE_BIKE_TRAINER)
            || (sportType == Workout.TYPE_ROWER)
            || (sportType == Workout.TYPE_CROSS_TRAINER)){
            int cadence = sourcePoint.hasCadence() ? sourcePoint.getCadencePoint().getCadence() : 0;
            int wheelRpm = sourcePoint.hasWheelData() ? sourcePoint.getCadencePoint().getWheelRpm() : 0;
            int heartrate = sourcePoint.hasHeartrate() ? sourcePoint.getHeartratePoint().getHeartrate() : 0;
            int indoorbikepower = sourcePoint.hasIndoorbike() ? sourcePoint.getIndoorbikePoint().getIndoorbikePower() : 0;
            int biketrainerpower = sourcePoint.hasBiketrainer() ? sourcePoint.getBiketrainerPoint().getBiketrainerPower(): 0;
            int biketrainercadence = sourcePoint.hasBiketrainer() ? sourcePoint.getBiketrainerPoint().getBiketrainerCadence(): 0;
            int crosstrainerpower = sourcePoint.hasCrosstrainer() ? sourcePoint.getCrosstrainerPoint().getCrosstrainerPower() : 0;
            int rowerpower = sourcePoint.hasRower() ? sourcePoint.getRowerPoint().getRowerPower() : 0;
            Log.logv(TAG, "isPause, cadence = " + cadence + ", wheel = " + wheelRpm + ", heartrate = " + heartrate);
            Log.logv(TAG, "isPause, indoorbikepower = " + indoorbikepower);
            Log.logv(TAG, "isPause, crosstrainerpower = " + crosstrainerpower);
            Log.logv(TAG, "isPause, rowerpower = " + rowerpower);
            Log.logv(TAG, "isPause, biketrainerpower = " + biketrainerpower);
            return cadence == 0 
                   && wheelRpm == 0 
                   && heartrate == 0 
                   && indoorbikepower == 0 
                   && crosstrainerpower == 0 
                   && rowerpower == 0 
                   && biketrainerpower == 0
                   && biketrainercadence == 0;
        }
        boolean isPaused = false;
        long duration = DurationUtil.get(sourcePoint.getTimestamp(), lastValidPoint.getTimestamp());
        Log.logv(TAG, "isPause, get timestamp duration = " + duration);
        if (sourcePoint.getSpeed() < getNotMovingSpeed(sportType)) {
            notMovingCount++;
            Log.logv(TAG, "isPause, notMovingCount=" + notMovingCount);
            if (notMovingCount > getNotMovingCount(sportType)) {
                isPaused = true;
            }
        } else {
            notMovingCount = 0;
        }
        lastValidPoint = sourcePoint;
        return isPaused;
    }

    public void release() {
        lastValidPoint = null;
        notMovingCount = 0;
    }
}